<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of formcreate
 *
 * @author gt
 */
define('AppRoot', dirname(dirname(dirname(__FILE__))));
include_once("../config/config.php");
include_once("../controller/cFormController.php");
include_once("../controller/cXMLController.php");
include_once("../smarty/Smarty.class.php");
$smarty = new Smarty();
$cFormCreate = new cFormController();
$xmlObj = new cXMLController();



$xmlObj->file = 'formdesigns/' . $_POST['page_settings']['tablenames'] . '_v2.xml';

if ($_POST['submit']) {

unset($_POST['submit']);
$cFormCreate->designerSource = $xmlObj->data = $_POST;

$cFormCreate->createView();
$cFormCreate->createScript();
$cFormCreate->createController();

$xmlObj->writeArrayToXML();

file_put_contents(AppRoot . AppScriptURL . $_POST['page_settings']['tablenames'] . "_v2.php", $cFormCreate->scriptCode);

file_put_contents(AppRoot . AppController . 'c' . ucwords($_POST['page_settings']['tablenames']) . "Controller_v2.php", $cFormCreate->controllerScript);
file_put_contents(AppRoot . AppLocalizationURL . $_POST['page_settings']['tablenames'] . "_v2.lang", json_encode($cFormCreate->localizationStrings[AppLang]));

file_put_contents($smarty->template_dir . "/" . $_POST['page_settings']['tablenames'] . "_v2.tpl", $cFormCreate->viewScript);



echo "configuration saved successfully !!!!";
exit;
} else {

$dbdata = $cFormCreate->getTableDetails();
$tabledata = $cFormCreate->getColumnDetails("__city");


foreach(array_keys($tabledata) as $columnname){
    $tabledatajson[]["label"]=$columnname;
}

$content_details_array['formelements']['dbdata']=  json_encode($data);
//$tabledatajson="[";
//foreach(array_keys($tabledata) as $columnname){
//    $tabledatajson.="{ label:'".$columnname."'},";
//}
//$tabledatajson=rtrim($tabledatajson,",");
//$tabledatajson.="]";
$content_details_array['formelements']['selectedtablescolumns']= json_encode($tabledatajson);



}

$smarty->assign('AppCssURL', AppCssURL);
$smarty->assign('AppImgURL', AppImgURL);
$smarty->assign('AppJsURL', AppJsURL);
$smarty->assign('AppTheme', AppTheme);
$smarty->assign('AppThemeCss', AppThemeCss);
$smarty->assign('AppThemeJs', AppThemeJs);
$smarty->assign('AppJqueryTheme', AppJqueryTheme);
$smarty->assign('AppThemeImg', AppThemeImg);
$smarty->assign('columns', $columns);
$smarty->assign('current_table_name', $current_table_name);
$smarty->assign('content_details_array', $content_details_array);

$smarty->display(AppTheme . 'header.tpl');
$smarty->display('formcreatev2.tpl');
$smarty->display(AppTheme . 'footer.tpl');



function makeDisplayName($columnName) {
return ucwords(str_replace('_', ' ', $columnName));
}

function setDefaultValues($savedcolumndata, $default = "") {
return $savedcolumndata ? $savedcolumndata : $default;
}


?>
